package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.license.utils.LicenseUtil;
import com.ruoyi.system.mapper.ProjectMapper;
import com.ruoyi.system.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.HtProject;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目信息Service业务层处理
 * 
 * @author zhz
 * @date 2024-03-22
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, HtProject> implements ProjectService
{
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询项目信息
     * 
     * @param id 项目信息主键
     * @return 项目信息
     */
    @Override
    public HtProject selectHtProjectById(String id)
    {
        return projectMapper.selectHtProjectById(id);
    }

    /**
     * 查询项目信息列表
     * 
     * @param htProject 项目信息
     * @return 项目信息
     */
    @Override
    public List<HtProject> selectHtProjectList(HtProject htProject)
    {
        return projectMapper.selectHtProjectList(htProject);
    }

    /**
     * 新增项目信息
     * 
     * @param htProject 项目信息
     * @return 结果
     */
    @Override
    public int insertHtProject(HtProject htProject)
    {
        htProject.setCreateTime(DateUtils.getNowDate());
        return projectMapper.insert(htProject);
    }

    /**
     * 修改项目信息
     * 
     * @param htProject 项目信息
     * @return 结果
     */
    @Override
    public int updateHtProject(HtProject htProject)
    {
        htProject.setUpdateTime(DateUtils.getNowDate());
        return projectMapper.updateHtProject(htProject);
    }

    /**
     * 批量删除项目信息
     * 
     * @param ids 需要删除的项目信息主键
     * @return 结果
     */
    @Override
    public int deleteHtProjectByIds(String[] ids)
    {
        return projectMapper.deleteHtProjectByIds(ids);
    }

    /**
     * 删除项目信息信息
     * 
     * @param id 项目信息主键
     * @return 结果
     */
    @Override
    public int deleteHtProjectById(String id)
    {
        return projectMapper.deleteHtProjectById(id);
    }

    @Override
    public String generaLicense(String id, Long endTime, String license) {
        Map<String, String> licenseInfo = LicenseUtil.getLicenseInfo(System.currentTimeMillis(), endTime, license);
        HtProject project = selectHtProjectById(id);
        project.setLicense(license);
        project.setLicensePrivateKey(licenseInfo.get("privateKey"));
        project.setLicensePublicKey(licenseInfo.get("publicKey"));
        // 保存公钥私钥到数据库
        projectMapper.updateHtProject(project);
        // 将公钥和授权文件放在同一个文件中  ===== 分割
        return licenseInfo.get("keyFile") + "====" + licenseInfo.get("publicKey");
    }
}
